<?php

class Kamar_Dao{

	var $host;
	var $user;
	var $pass;
	var $dbnm;
	var $db;
	
	function __construct($host,$user,$pass,$dbnm){
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		$this->dbnm = $dbnm;
	}
	
	function _connect(){
		$this->db = mysql_connect($this->host,$this->user,$this->pass);
		mysql_select_db($this->dbnm,$this->db);
	}
	
	function _close(){
		mysql_close($this->db);
	}

	function add(Kamar $kamar){
		
		$this->_connect();
			
		$sql = "INSERT INTO `gemuk`.`kamar` 
			(
				`rooms_no`, `id_pengguna`, `rooms_name`, `price`, `facility`, `address`, `roomspace`, `location`, `room_figure`
			)
			VALUES
			(
				NULL,
				'".$kamar->id_pengguna."',
				'".$kamar->rooms_name."',
				'".$kamar->price."',
				'".$kamar->facility."',
				'".$kamar->address."',
				'".$kamar->roomspace."',
				'".$kamar->map_lock."',
				'".$kamar->room_figure."'
			);";
		
		mysql_query($sql,$this->db);
		
		$kamar->rooms_no = mysql_insert_id($this->db);
		
		$this->_close();	
	}
	
	function get($rooms_no){
	
		$this->_connect();
		
		$sql = "SELECT * FROM kamar WHERE rooms_no = '".$rooms_no."'";
		
		$kamar = false;
		
		$res = mysql_query($sql,$this->db);
		
		if(mysql_num_rows($res)){
		
			$row = mysql_fetch_assoc($res);
		
			$kamar = new Kamar();
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];
		}
		
		$this->_close();
		
		return $kamar;
	}
		
	function get_all($Id_pengguna){
	
		$this->_connect();
		
		$sql = "SELECT * FROM kamar WHERE id_pengguna = '".$Id_pengguna."' ORDER BY rooms_no";
		
		$list_kamar = array();
		
		$res = mysql_query($sql,$this->db);
		
		if(mysql_num_rows($res)){
			while($row = mysql_fetch_assoc($res)){
			
				$kamar = new kamar();				
				
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];	
				
				$list_kamar[] = $kamar;			
			}
		}
		
		$this->_close();
		
		return $list_kamar;	
	}
	
	function delete($rooms_no){
	
		$this->_connect();
		
		$sql = "DELETE FROM	`gemuk`.`kamar` WHERE rooms_no = '".$rooms_no."'";
		
		mysql_query($sql,$this->db);
		
		$this->_close();
	}
	
	function update(kamar $kamar){
	
		$this->_connect();
		
		$sql = "UPDATE `gemuk`.`kamar` SET
				`rooms_no`		= '".$kamar->rooms_no."',
				`id_pengguna` 	= '".$kamar->id_pengguna."',
				`rooms_name`	= '".$kamar->rooms_name."',
				`price`			= '".$kamar->price."',
				`facility`		= '".$kamar->facility."',
				`address`		= '".$kamar->address."',
				`roomspace`		= '".$kamar->roomspace."',
				`map_lock`		= '".$kamar->map_lock."',
				`room_figure`	= '".$kamar->room_figure."',
			WHERE `pengguna`.`rooms_no` = '".$kamar->rooms_no."'";
		
		mysql_query($sql,$this->db);
		
		$this->_close();
	}
	
	function unggah_photo(){
		
		$hasil = '';
		
		//proses pemeriksaan photo
		$lokasi_file    = $_FILES['fupload']['tmp_name'];
		$tipe_file      = $_FILES['fupload']['type'];
		$nama_file      = $_FILES['fupload']['name'];
		$nama_size      = $_FILES['fupload']['size'];
		$acak           = rand(000,999);
		$nama_file_unik = $acak.$nama_file;
		
		//Apabila ada photo yang diupload
		if (!empty($lokasi_file)){
		// Apabila tipe gambar bukan jpeg akan tampil peringatan
		if ($tipe_file != "image/jpeg" and $tipe_file != "image/pjpeg"){
			$hasil = "pendaftaran gagal karena photo harus berformat jpg";
		}else{
			if($nama_size>3000000){
				$hasil = "pendaftaran gagal karena photo terlalu besar";
			}else{
				//proses menyimpan pendaftaran pengunjung
				move_uploaded_file($lokasi_file,"upload/$nama_file_unik");
				$hasil = $nama_file_unik;
			}
		}
		}else{
			$hasil = "photo harus disertakan";
		}
		return $hasil;
	}
	function search_all(){
	
		$this->_connect();
		
		$sql = "SELECT * FROM kamar ORDER BY rooms_no";
		
		$list_kamar = array();
		
		$res = mysql_query($sql,$this->db);
		if(mysql_num_rows($res)){
			while($row = mysql_fetch_assoc($res)){
			
				$kamar = new kamar();				
				
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];	
				
				$list_kamar[] = $kamar;			
			}
		}
		
		$this->_close();
		
		return $list_kamar;	
	}

	function search_location($location){
	
		$this->_connect();
		if($location=='other'){
		$sql = "SELECT * FROM kamar WHERE address not LIKE '%gebang%' and
											address not LIKE '%mulyosari%' and
											address not LIKE '%keputih%' ORDER BY rooms_no";
		}else{
		$sql = "SELECT * FROM kamar WHERE address LIKE '%".$location."%' ORDER BY rooms_no";
		}
		
		$list_kamar = array();
		
		$res = mysql_query($sql,$this->db);
		if(mysql_num_rows($res)){
			while($row = mysql_fetch_assoc($res)){
			
				$kamar = new kamar();				
				
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];	
				
				$list_kamar[] = $kamar;			
			}
		}
		
		$this->_close();
		
		return $list_kamar;	
	}

	function search_price($price){
	
		$this->_connect();
		switch($price){
		case 'price1' : {
		$sql = "SELECT * FROM kamar WHERE price < 200000 ORDER BY rooms_no";
			break;}
		
		case 'price2' : {
		$sql = "SELECT * FROM kamar WHERE price > 199999 AND price < 400001 ORDER BY rooms_no";
			break;}

		case 'price3' : {
		$sql = "SELECT * FROM kamar WHERE price > 399999 AND price < 500001 ORDER BY rooms_no";
			break;}		
		
		case 'price4' : {
		$sql = "SELECT * FROM kamar WHERE price > 499999 AND price < 1000001 ORDER BY rooms_no";
			break;}	
		
		case 'price5' : {
		$sql = "SELECT * FROM kamar WHERE price > 1000000 ORDER BY rooms_no";
			break;}	
		
		}	
			
		$list_kamar = array();
		
		$res = mysql_query($sql,$this->db);
		if(mysql_num_rows($res)){
			while($row = mysql_fetch_assoc($res)){
			
				$kamar = new kamar();				
				
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];	
				
				$list_kamar[] = $kamar;			
			}
		}
		
		$this->_close();
		
		return $list_kamar;	
	}
	
		function search_facility($facility){
	
		$this->_connect();
		$sql = "SELECT * FROM kamar WHERE facility LIKE '%".$facility."%' ORDER BY rooms_no";
		
		$list_kamar = array();
		
		$res = mysql_query($sql,$this->db);
		if(mysql_num_rows($res)){
			while($row = mysql_fetch_assoc($res)){
			
				$kamar = new kamar();				
				
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];	
				
				$list_kamar[] = $kamar;			
			}
		}
		
		$this->_close();
		
		return $list_kamar;	
	}
	
		function search_roomspace($roomspace){
	
		$this->_connect();
		$sql = "SELECT * FROM kamar WHERE roomspace LIKE '%".$roomspace."%' ORDER BY rooms_no";
		
		
		$list_kamar = array();
		
		$res = mysql_query($sql,$this->db);
		if(mysql_num_rows($res)){
			while($row = mysql_fetch_assoc($res)){
			
				$kamar = new kamar();				
				
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];	
				
				$list_kamar[] = $kamar;			
			}
		}
		
		$this->_close();
		
		return $list_kamar;	
	}
	
	function search_owner($owner){
	
		$this->_connect();
		$sql = "SELECT a.* FROM kamar a LEFT OUTER JOIN pengguna b ON a.id_pengguna=b.id_pengguna WHERE LOWER(b.name) LIKE LOWER('%".$owner."%') ORDER BY rooms_no";
		$list_kamar = array();
		
		$res = mysql_query($sql,$this->db);
		if(mysql_num_rows($res)){
			while($row = mysql_fetch_assoc($res)){
			
			$kamar = new kamar();				
				
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];	
				
			$list_kamar[] = $kamar;			
			}
		}
		
		$this->_close();
		
		return $list_kamar;	
	}
	
		function search_other($other){
		$this->_connect();
		$sql = "SELECT a.* FROM kamar a LEFT OUTER JOIN pengguna b ON a.id_pengguna=b.id_pengguna WHERE LOWER(b.name) LIKE LOWER('%".$other."%') 
		OR LOWER(a.address) LIKE LOWER('%".$other."%')
		OR LOWER(a.price) LIKE LOWER('%".$other."%')
		ORDER BY rooms_no";
		$list_kamar = array();
		
		$res = mysql_query($sql,$this->db);
		if(mysql_num_rows($res)){
			while($row = mysql_fetch_assoc($res)){
			
			$kamar = new kamar();				
				
			$kamar->rooms_no  	 = $row['rooms_no'];
			$kamar->id_pengguna  = $row['id_pengguna'];
			$kamar->rooms_name   = $row['rooms_name'];
			$kamar->price		 = $row['price'];
			$kamar->facility     = $row['facility'];
			$kamar->address      = $row['address'];
			$kamar->roomspace	 = $row['roomspace'];
			$kamar->map_lock     = $row['map_lock'];	
			$kamar->room_figure  = $row['room_figure'];	
				
			$list_kamar[] = $kamar;			
			}
		}
		
		$this->_close();
		
		return $list_kamar;	
	}
	
}

class Kamar{

	var $rooms_no	  = '';
	var $id_pengguna  = '';
	var $rooms_name   = '';
	var $price   	  = '';
	var $facility     = '';
	var $address      = '';
	var $roomspace	      = '';
	var $map_lock     = '';
	var $rooms_figure = '';
	
	
}